//
//  VCLoginMessage.m
//  MOSAD_HW2_1
//
//  Created by Jason on 2020/10/29.
//  Copyright © 2020 Jason. All rights reserved.
//

#import "VCLoginMessage.h"

@interface VCLoginMessage ()

@end

@implementation VCLoginMessage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"登录";
    self.navigationController.navigationBar.translucent = NO;
    //设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //设置登录界面内容
    //用户名提示标签创建
    _lbUserName = [[UILabel alloc] init];
    _lbUserName.frame = CGRectMake(20, 60, 80, 40);
    _lbUserName.text = @"用户名:";
    _lbUserName.font = [UIFont systemFontOfSize:24];
    _lbUserName.textAlignment = NSTextAlignmentLeft;
    
    //用户密码提示
    _lbPassword = [[UILabel alloc] init];
    _lbPassword.frame = CGRectMake(20, 144, 80, 40);
    _lbPassword.text = @"密码:";
    _lbPassword.font = [UIFont systemFontOfSize:24];
    _lbPassword.textAlignment = NSTextAlignmentLeft;
    
//    //邮箱
//    _lbEmail = [[UILabel alloc] init];
//    _lbEmail.frame = CGRectMake(20, 228, 80, 40);
//    _lbEmail.text = @"邮箱:";
//    _lbEmail.font = [UIFont systemFontOfSize:24];
//    _lbEmail.textAlignment = NSTextAlignmentLeft;
//
//    //电话
//    _lbPhoneNum = [[UILabel alloc] init];
//    _lbPhoneNum.frame = CGRectMake(20, 312, 80, 40);
//    _lbPhoneNum.text = @"电话:";
//    _lbPhoneNum.font = [UIFont systemFontOfSize:24];
//    _lbPhoneNum.textAlignment = NSTextAlignmentLeft;
    
    //用户名输入框
    _tfUserName = [[UITextField alloc] init];
    _tfUserName.frame = CGRectMake(120, 60, self.view.bounds.size.width-140, 40);
    _tfUserName.placeholder = @"请输入用户名";
    _tfUserName.borderStyle = UITextBorderStyleRoundedRect;
    
    //密码输入框
    _tfPassword = [[UITextField alloc] init];
    _tfPassword.frame = CGRectMake(120, 144, self.view.bounds.size.width-140, 40);
    _tfPassword.placeholder = @"请输入密码";
    _tfPassword.borderStyle = UITextBorderStyleRoundedRect;
    //密码是否保密
    _tfPassword.secureTextEntry = YES;
    
//    //邮箱输入框
//    _tfEmail = [[UITextField alloc] init];
//    _tfEmail.frame = CGRectMake(120, 228, self.view.bounds.size.width-140, 40);
//    _tfEmail.placeholder = @"请输入邮箱地址";
//    _tfEmail.borderStyle = UITextBorderStyleRoundedRect;
//
//    //电话输入框
//    _tfPhoneNum = [[UITextField alloc] init];
//    _tfPhoneNum.frame = CGRectMake(120, 312, self.view.bounds.size.width-140, 40);
//    _tfPhoneNum.placeholder = @"请输入电话号码";
//    _tfPhoneNum.borderStyle = UITextBorderStyleRoundedRect;
    
    //登录和注册btn创建
    _btLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btLogin.frame = CGRectMake(0, 0, 80, 80);
    _btLogin.titleLabel.font = [UIFont systemFontOfSize:24];
    _btLogin.center = CGPointMake(self.view.bounds.size.width/2, 350);
    [_btLogin setTitle:@"登录" forState:UIControlStateNormal];
    [_btLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];

    _btRegister = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btRegister.frame = CGRectMake(0, 0, 80, 80);
    _btRegister.titleLabel.font = [UIFont systemFontOfSize:24];
    _btRegister.center = CGPointMake(self.view.bounds.size.width/2, 430);
    [_btRegister setTitle:@"注册" forState:UIControlStateNormal];
    [_btRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_lbUserName];
    [self.view addSubview:_lbPassword];
//    [self.view addSubview:_lbEmail];
//    [self.view addSubview:_lbPhoneNum];
    [self.view addSubview:_tfUserName];
    [self.view addSubview:_tfPassword];
//    [self.view addSubview:_tfEmail];
//    [self.view addSubview:_tfPhoneNum];
    [self.view addSubview:_btLogin];
    [self.view addSubview:_btRegister];

    
    NSInteger a = 0;
    NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:@"Jason" forKey:[NSString stringWithFormat:@"USERNAME%ld", a]];
    [ud setObject:@"666666" forKey:[NSString stringWithFormat:@"PASSWORD%ld", a]];
    [ud setObject:@"123456@qq.com" forKey:[NSString stringWithFormat:@"EMAIL%ld", a]];
    [ud setObject:@"123456789" forKey:[NSString stringWithFormat:@"PHONENUM%ld", a]];
    
}

//登录事件
-(void) pressLogin {
    
    AppDelegate * myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
//    NSString * textUserName = _tfUserName.text;
//    NSString * textEmail = _tfEmail.text;
//    NSString * textPhoneNum = _tfPhoneNum.text;
    
    VCMy * vcMy = [[VCMy alloc] init];
    //通过属性传参
//   vcMy.userName = textUserName;
//    vcMy.email = textEmail;
//    vcMy.phoneNum = textPhoneNum;
    
    if(_tfUserName.text.length == 0 || _tfPassword.text.length == 0) {
        UIAlertAction * OK = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
        if(_tfUserName.text.length == 0) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"用户名未填写!" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:OK];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else if(_tfPassword.text.length == 0) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"密码未填写!" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:OK];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        
    }
    else {
        
        //判断用户名和账号是否正确
        BOOL pass = NO;
        
        for(int i = 0; i < myDelegate.number; i++) {
            NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
            id userName = [ud objectForKey:[NSString stringWithFormat:@"USERNAME%d", i]];
            NSString * userNameStr = (NSString *) userName;
            id password = [ud objectForKey:[NSString stringWithFormat:@"PASSWORD%d", i]];
            NSString * passwordStr = (NSString *) password;
            id email = [ud objectForKey:[NSString stringWithFormat:@"EMAIL%d", i]];
            NSString * emailStr = (NSString *) email;
            id phoneNum = [ud objectForKey:[NSString stringWithFormat:@"PHONENUM%d", i]];
            NSString * phoneNumStr = (NSString *) phoneNum;
            
            if([_tfUserName.text isEqualToString:userNameStr] && [_tfPassword.text isEqualToString:passwordStr]){
                vcMy.userName = userNameStr;
                vcMy.email = emailStr;
                vcMy.phoneNum = phoneNumStr;
                [self.navigationController pushViewController:vcMy animated:YES];
                pass = YES;
                
                break;
            }
            
        }
        
        if(!pass) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"用户名或密码输入错误!" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * OK = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:OK]; 
            [self presentViewController:alertController animated:YES completion:nil];
        }
    
    }
    
    
}

-(void) pressRegister {
    VCRegister * vcRegister = [[VCRegister alloc] init];
    [self.navigationController pushViewController:vcRegister animated:YES];
}



-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //回收键盘对象
    [_tfUserName resignFirstResponder];
    [_tfPassword resignFirstResponder];
}

@end
